﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace YFINGenerator
{
    public partial class Factory
    {
        public static string ServiceGenerator(EntityInfo entityInfo)
        {
            string res = "";
            res += @"//
//此代码由YFINGenerator自动生成
//Copyright (c) 段凯强 2013~2014
//" + DateTime.Now + @"
//
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using YFIN.Data;
using YFIN.Helper.Extension;
using YFIN.Models;

namespace YFIN.Service." + entityInfo.Directory + @"
{
    public class " + entityInfo.Name.Remove(0, 5) + @"Service : I" + entityInfo.Name.Remove(0, 5) + @"Service
    {";
            res += AddGenerator(entityInfo);
            res += GetGenerator(entityInfo);
            res += GetByForeignKey(entityInfo);
            res += UpdateGenerator(entityInfo);
            res += DeleteGenerator(entityInfo);
            res += @"    }
}";
            return res;
        }


        public static string AddGenerator(EntityInfo entityInfo)
        {
            string v = entityInfo.Name.Remove(0, 5);
            v = v.Insert(1, v[0].ToString().ToLower());
            v = v.Remove(0, 1);
            return @"
        /// <summary>
        /// 添加一个" + entityInfo.Remark +
                   @"
        /// </summary>
        /// <param name=" + "\"" + v + "\">要添加的" + entityInfo.Remark +
                   @"</param>
        public bool Add(" + entityInfo.Name.Replace("YFIN_", "Model") + " " + v +
                   @")
        {
            try
            {
                YFINExtension.GoAndSave(entities => entities." +
                   entityInfo.Name + @".AddObject(" + entityInfo.Name.Replace("YFIN_", "Model") +
                   @".ConvertTo" + entityInfo.Name + @"(" + v +
                   @")));
                return true;
            }
            catch(Exception e)
            {
                return false;
            }
        }

";
        }

        public static string GetGenerator(EntityInfo entityInfo)
        {
            string v = entityInfo.Key;
            v = v.Insert(1, v[0].ToString().ToLower());
            v = v.Remove(0, 1);
            return @"
        /// <summary>
        /// 通过" + v + @"查找" + entityInfo.Remark +
                   @"
        /// </summary>
        /// <param name=" + "\"" + v + "\">" + v +
                   @"</param>
        /// <returns>找到的" + entityInfo.Remark + @"</returns>
        public " +
                   entityInfo.Name.Replace("YFIN_", "Model") + @" GetBy" + entityInfo.Key + @"(" + entityInfo.KeyType + @" " + v +
                   @")
        {
            return " + entityInfo.Name.Replace("YFIN_", "Model") + @".ConvertTo" + entityInfo.Name.Replace("YFIN_", "Model") +
                     @"(YFINExtension.Go(entities => entities." +
                   entityInfo.Name + @".SingleOrDefault(" + v[0] + @" => " + v[0] + @"." + entityInfo.Key + @" == " + v +
                   @")));
        }

";
        }

        public static string GetByForeignKey(EntityInfo entityInfo)
        {
            string res = @"";
            string t = entityInfo.Name.Remove(0, 5).Substring(0, 1).ToLower();
            foreach (var e in entityInfo.ForeighKey)
            {
                string vfk = e.Substring(0, e.IndexOf(':'));
                vfk = vfk.Insert(1, vfk[0].ToString().ToLower());
                vfk = vfk.Remove(0, 1);
                res += @"
        /// <summary>
        /// 根据" + e.Substring(0, e.IndexOf(':')) + "查找所有" +
                       entityInfo.Remark +
                       @"
        /// </summary>
        /// <param name=" + "\"" +
                       vfk +
                       "\">" + e.Substring(0, e.IndexOf(':')) + @"</param>
        /// <returns>找到的" +
                       entityInfo.Remark +
                       @"</returns>
        public List<" +
                       entityInfo.Name.Replace("YFIN_", "Model") + "> GetBy" + e.Substring(0, e.IndexOf(':'))
                       + "(" + e.Substring(e.IndexOf(':') + 1) + " " + vfk +
                       @")
        {
            return YFINExtension.Go(entities => entities." + entityInfo.Name +
                       ".Where(" + t + " => " + t + "." + e.Substring(0, e.IndexOf(':')) + " == " + vfk +
                       @").ToList()).Select(
                    " + entityInfo.Name.Replace("YFIN_", "Model") +
                       ".ConvertTo" + entityInfo.Name.Replace("YFIN_", "Model") + @").ToList();
        }
";
            }
            return res;
        }

        public static string UpdateGenerator(EntityInfo entityInfo)
        {
            string v = entityInfo.Name.Remove(0, 5);
            v = v.Insert(1, v[0].ToString().ToLower());
            v = v.Remove(0, 1);
            return @"
        /// <summary>
        /// 更新" + entityInfo.Remark +
                   @"信息
        /// </summary>
        /// <param name=" + "\"new" + entityInfo.Name.Remove(0, 5) +
                   "\">更新后的" + entityInfo.Remark +
                   @"</param>
        /// <returns>是否成功</returns>
        public bool Update(" +
                   entityInfo.Name.Replace("YFIN_", "Model") + " new" + entityInfo.Name.Remove(0, 5) +
                   @")
        {
            try
            {
                YFINExtension.GoAndSave(
                    entities =>
                        {
                            var " +
                   v + " = entities." + entityInfo.Name + ".SingleOrDefault(" + v[0] + " => " + v[0] + "." +
                   entityInfo.Key + " == new" + entityInfo.Name.Remove(0, 5) + "." + entityInfo.Key +
                   @");
                            " + entityInfo.Name.Replace("YFIN_", "Model") + ".CopyTo" + entityInfo.Name + "(" + v + ", new" +
                   entityInfo.Name.Remove(0, 5) +
                   @");
                        });
                return true;
            }
            catch (Exception e)
            {
                return false;
            }
        }

";
        }

        public static string DeleteGenerator(EntityInfo entityInfo)
        {
            string v = entityInfo.Name.Remove(0, 5);
            v = v.Insert(1, v[0].ToString().ToLower());
            v = v.Remove(0, 1);
            return @"
        /// <summary>
        /// 删除" + entityInfo.Remark +
                   @"
        /// </summary>
        /// <param name=" + "\"" + v + "\">要删除的" + entityInfo.Remark +
                   @"</param>
        /// <returns>是否成功</returns>
        public bool Delete(" +
                   entityInfo.Name.Replace("YFIN_", "Model") + " " + v +
                   @")
        {
            try
            {
                YFINExtension.GoAndSave(entities =>
                                        entities." +
                   entityInfo.Name + @".DeleteObject(
                                            entities." +
                   entityInfo.Name + @".SingleOrDefault(" + v[0] + @" => " + v[0] + @"." + entityInfo.Key + @" == " + v +
                   @"." + entityInfo.Key +
                   @")));
                return true;
            }
            catch (Exception e)
            {
                return false;
            }
        }

";
        }
    }
}
